<?php
//
//	file: inc/tree.list.class.php
//	author: Pierre ROGE pour GAIA (gaia.fr)
//	begin: 15/02/2008
//	version: 0.0.1 - 15/02/2008
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//

if ( !defined('SYS') )
{
	die('Not allowed');
}

class sys_tree_list extends sys_stdclass
{
	var $action;
	var $menu_root;
	var $tree;

	function __construct($api_name)
	{
		parent::__construct($api_name);
		$this->action = false;
		$this->menu_root = false;
		$this->tree = false;
	}

	function __destruct()
	{
		if ( isset($this->tree) )
		{
			sys::kill($this->tree);
			unset($this->tree);
		}
		unset($this->tree_locale);
		unset($this->menu_root);
		unset($this->action);
		parent::__destruct();
	}

	function set()
	{
		$this->menu_root = 'main';
		$this->tree = false;
		$this->set_tree();
	}

	function process()
	{
		$api = &$GLOBALS[$this->api_name];

		// we deal with open/close there
		$item_id = $this->get_action();
		if ( $this->action && in_array($this->action, array('attachup', 'attachdw')) )
		{
			$this->tree->attach($item_id, $this->action == 'attachup');
			$this->action = false;
		}
		if ( $this->action && in_array($this->action, array('moveup', 'movedw')) )
		{
			$this->tree->move($item_id, $this->action == 'moveup');
			$this->action = false;
		}
		if ( $this->action && in_array($this->action, array('open', 'close')) )
		{
			$this->tree->open_close_branch($item_id, $this->action == 'close');
			$this->action = false;
		}

		// if we have an action, we process it and end there
		if ( $this->action )
		{
			$res = $api->menus->process(implode('.', array($this->menu_root, $api->mode, $this->action)));
			if ( $res !== false )
			{
				return $res;
			}
		}

		// standard process
		if ( $this->init() )
		{
			$this->check();
			$this->validate();
			return $this->display();
		}
		return false;
	}

	function init()
	{
		$api = &$GLOBALS[$this->api_name];

		// read the tree
		$this->tree->read_open();
		return true;
	}

	function check() {}
	function validate() {}

	function get_action()
	{
		$sys = &$GLOBALS[SYS];
		$io = &$sys->io;

		// identify action
		$this->action = false;
		$item_id = false;
		$buttons = $this->get_buttons();
		foreach ( $buttons as $button => $with_id )
		{
			if ( $io->button($button) && (!$with_id || ($item = $io->read($button, array(0)))) )
			{
				$this->action = $button;
				$item_id = $with_id ? key($item) : false;
				break;
			}
		}

		// we didn't push a button: verify if an action comes from url
		if ( !$this->action )
		{
			$this->action = $io->read(SYS_U_ACTION, '');
			$item_id = $io->read(SYS_U_ITEM, 0);
		}
		return $item_id;
	}

	function get_buttons()
	{
		// action => on row, eg with id
		return array(
			'close' => true,
			'open' => true,
			'moveup' => true,
			'movedw' => true,
			'attachup' => true,
			'attachdw' => true,
			'edit' => true,
			'delete' => true,
			'create' => false,
		);
	}

	function display($id=false)
	{
		$sys = &$GLOBALS[SYS];
		$api = &$GLOBALS[$this->api_name];
		$tpl = &$sys->tpl;

		// display the tree
		$keys = array_keys($this->tree->data);
		$count_keys = count($keys);

		// then walk the tree
		$stack = array();
		for ( $i = 1; $i < $count_keys; $i++ )
		{
			// if rid > parent.rid, we have finished with this parent, so remove it from the stack
			$count_close = 0;
			while ( ($count_stack = count($stack)) && ($this->tree->data[ $keys[$i] ][$this->tree->field_rid] > $this->tree->data[ $stack[ ($count_stack - 1) ] ][$this->tree->field_rid]) )
			{
				array_pop($stack);
				$count_close++;
			}
			if ( $i > 1 )
			{
				for ( $j = $count_close; $j > 1; $j-- )
				{
					$tpl->add('row.close');
				}
			}

			// send to tpl
			$tpl_data = $this->get_option($keys[$i]);
			$pid = $keys[$i] ? $this->tree->data[ $keys[$i] ][$this->tree->field_pid] : false;
			if ( !isset($tpl_data['IS_OPENED']) && ($i + 1 < $count_keys) && ($this->tree->data[ $keys[$i + 1] ][$this->tree->field_rid] < $this->tree->data[ $keys[$i] ][$this->tree->field_rid]) )
			{
				$tpl_data['IS_OPENED'] = true;
			}
			if ( !isset($tpl_data['CAN_MOVE_UP']) && ($this->tree->data[ $keys[$i] ][$this->tree->field_lid] > $this->tree->data[$pid][$this->tree->field_lid] + 1) )
			{
				$tpl_data['CAN_MOVE_UP'] = true;
			}
			if ( !isset($tpl_data['CAN_MOVE_DW']) && ($this->tree->data[ $keys[$i] ][$this->tree->field_rid] < $this->tree->data[$pid][$this->tree->field_rid] - 1) )
			{
				$tpl_data['CAN_MOVE_DW'] = true;
			}
			if ( !isset($tpl_data['CAN_ATTACH_UP']) && $pid )
			{
				$tpl_data['CAN_ATTACH_UP'] = true;
			}
			if ( !isset($tpl_data['CAN_ATTACH_DW']) && ($this->tree->data[ $keys[$i] ][$this->tree->field_lid] > $this->tree->data[$pid][$this->tree->field_lid] + 1) )
			{
				$tpl_data['CAN_ATTACH_DW'] = true;
			}
			$tpl->add('row', $tpl_data);
			$stack[] = $keys[$i];
		}
		// close remaining level
		for ( $j = count($stack); $j > 1; $j-- )
		{
			$tpl->add('row.close');
		}
	}

	function get_option($id)
	{
	}

	function set_tree()
	{
		$this->tree = false;
	}
}

?>